import socket
import person_pb2
import datetime

def all_bytes():
    person = person_pb2.Person()
    person.name = send_name
    person.time = send_time
    person.show = send_show
    # 序列化
    return person.SerializeToString()

if __name__ == '__main__':
    ip = '127.0.0.1'
    port = 9998
    # 1.创建tcp服务端套接字
    socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 2.绑定端口号
    socket_server.bind((ip, port))
    # 3.设置监听
    socket_server.listen(5)
    # 4.等待接收客户端的连接请求
    conn, addr = socket_server.accept()
    while True:
        # 接收信息
        Head_data = conn.recv(1024)
        tmessage = person_pb2.Person()
        # 将信息反序列化
        tmessage.ParseFromString(Head_data)
        m_name = tmessage.name
        m_time = tmessage.time
        m_show = tmessage.show
        print(f'姓名:{m_name}在{m_time}时说{m_show}')
        send_name = input("姓名")
        send_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        send_show = input("说:")
        hearBeat_data = all_bytes()
        # 3.将数据发送到客户务端
        conn.send(hearBeat_data)


    conn.close()
    # 7.关闭服务端套接字,表示不再等待接收客户端的连接请求
    socket_server.close()